<template>
<div class="container">
    <div class="page-head">
        <div class="left">
            <el-form :inline="true" :model="query" @submit.native.prevent>
                <el-form-item>
                    <el-button type="primary" @click="showCreateView"><i class="el-icon-plus" style="margin-right: 5px"></i>添加</el-button>
                </el-form-item>

                <el-form-item>
                    <el-input v-model="query.keyword" placeholder="UUID/设备名称" clearable></el-input>
                </el-form-item>
                <el-button type="primary" native-type="submit" @click="getTableData">搜索</el-button>
                <el-button type="warning" @click="resetQuery">重置</el-button>
            </el-form>
        </div>
    </div>

    <el-table ref="tableView" class="page-body" :data="tableData" height="tableHeight" border>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column label="设备ID" prop="id" width="100" align="center"></el-table-column>
        <el-table-column label="设备名称" prop="title" align="center"></el-table-column>
        <el-table-column label="UUID" prop="uuid" align="center"></el-table-column>
        <el-table-column label="操作" width="300" align="center">
            <template slot-scope="scope">
                <el-button type="primary" @click="showUpdateView(scope.row)">编辑</el-button>
                <el-button type="danger" @click="confirmDelete(scope.row.id)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <div class="page-foot">
        <div class="left"></div>
        <div class="right">
            <common-pagination :total="pg.total" :page.sync="pg.page" :limit.sync="pg.limit" @getList="getTableData"></common-pagination>
        </div>
    </div>

    <device-create ref="createViewRef" @success="getTableData"></device-create>
    <device-update ref="updateViewRef" @success="getTableData"></device-update>
</div>
</template>

<script>
import pagination from "@/components/mixins/pagination.vue";
import pickerView from "@/components/picker/index.vue";
import DeviceCreate from "./components/DeviceCreate.vue";
import DeviceUpdate from "./components/DeviceUpdate.vue";

// 设备相关api
import {
    getListApi,
    deleteApi
} from "@/api/task/device";


export default {
    components: {
        pickerView,
        DeviceCreate,
        DeviceUpdate
    },
    mixins: [pagination],
    data() {
        return {
            query: {
                keyword: ""
            },
            tableData: []
        }
    },
    created() {
        this.getTableData();
    },
    methods: {
        getTableData() {
            let params = Object.assign({}, this.query);
            params["pagination"] = this.pg;
            getListApi(params).then(res => {
                this.tableData = res.data.list;
                this.setPg(res);
            });
        },
        doSearch() {
            this.resetPg();
            this.getTableData();
        },
        resetQuery() {
            this.query = {
                cate_id: "",
                keyword: ""
            };
            this.getTableData();
        },
        showCreateView() {
            // 显示创建页面
            this.$refs.createViewRef.showCreateView()
        },
        showUpdateView(row) {
            this.$refs.updateViewRef.showUpdateView(row)
        },
        confirmDelete(id) {
            this.$confirm(`确认删除该设备吗？`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteApi({
                    id: id
                }).then(() => {
                    this.getTableData();
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                });         
                
            }).catch((e) => {
                console.log(e)
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        }
    }
}
</script>

<style>

</style>
